package com.feicheng.house.controller;


import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.feicheng.house.entity.Menjin;
import com.feicheng.house.entity.ShopMenjinVo;
import com.feicheng.house.rc4.RC4Util;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.service.IMenjinService;
import com.feicheng.house.service.IShopMenjinVoService;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author feichengTech
 * @since 2021-11-06
 */
@RestController
@RequestMapping("/house/shop-menjin-vo")
public class ShopMenjinVoController {
	@Autowired
	IMenjinService iMenjinService;
	@Autowired
	IShopMenjinVoService iShopMenjinVoService;
	@Autowired
	RC4Util rc4Util;
	/**
	 * 查询门禁基本信息并分页
	 * @param searchKey 模糊查询条件
 	 * @param currentPage 当前页
	 * @param pageSize 每页有几条
	 * @return 门禁集合
	 */
	 @GetMapping("/page")
	 public ResultInfo selectSMVoInfo(String searchKey,@RequestParam(required = false,defaultValue = "1")Integer currentPage,
	   @RequestParam(required = false,defaultValue = "4")Integer pageSize) {
	  return iShopMenjinVoService.selectInfo(searchKey, pageSize, currentPage);
	 }
	/**
	 * 查询门禁详情
	 * @param menjinId 门禁id
	 * @return 门禁对象
	 */
	@GetMapping("/info")
	ResultInfo one(Integer menjinId){
		return iShopMenjinVoService.get(menjinId);
	}
	/**
	 * 添加门禁基本信息
	 * @param shopMenjinVo 门禁对象
	 * @return boolean
	 */
	@PostMapping("/info")
	ResultInfo add(Menjin menjin) {
		return iShopMenjinVoService.add(menjin);
	}
	/**
	 * 修改门禁基本信息
	 * @param shopMenjinVo 门禁对象
	 * @return boolean
	 */
	@PutMapping("/info")
	ResultInfo change(ShopMenjinVo shopMenjinVo) {
		return iShopMenjinVoService.change(shopMenjinVo);
	}
	/**
	 * 删除门禁基本信息
	 * @param menjinId 门禁id
 	 * @return boolean
	 */
	@DeleteMapping("/info")
	ResultInfo delInfo(Integer menjinId) {
		return iShopMenjinVoService.del(menjinId);
	}
	
	@RequestMapping(value = "/createQrCode")
	@ResponseBody
	public ResponseEntity<byte[]> createQrCode(String content) throws UnsupportedEncodingException{
		String aa = "[999985,V123,20180225102513,20180226102513]";
		String s = rc4Util.encryRC4String(aa, rc4Util.KEY, "UTF-8");
		content = "CB01" + s;
		byte[] qrcode = null;
		ResponseEntity<byte[]> aEntity = null;
		try {
			if (!"".equals(content)) {
				qrcode = iShopMenjinVoService.getQRCodeImage(content, 350, 350);
				// Set headers
				final HttpHeaders headers = new HttpHeaders();
				headers.setContentType(MediaType.IMAGE_PNG);
				aEntity = new ResponseEntity<byte[]>(qrcode, headers, HttpStatus.CREATED);
			} else {
				System.out.println("二维码参数为空");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return aEntity;
	}
	
}

